<?php
namespace app\index\controller\sunfc;
use app\index\controller\Base;
use think\Db;
use app\index\controller\Pay;
header("Content-type:text/html;charset=utf-8");

class Passenger  extends Base
{
    //乘客首页
    public function Index(){

        //拿到行程中数据
        if(input('order_id')){
            //拿到行程中数据
            //此处拿的是司机users表的电话(走预约的)
            $trip_data=Db::name('dd_publish_orders')->alias('o')
                ->join('iw_dd_users u','u.uid=o.driver_id')
                ->join('iw_dd_publish p','o.publish_id=p.id')
                ->field('u.headimgurl,u.nickname,u.trust_rank,u.car_info,u.tel,u.sex,
                            p.depart_time,p.region,p.begin_place,p.begin_detail,p.end_place,p.end_detail,p.status,                         
                            o.number,o.id')
                ->where('o.id', input('order_id'))
                ->find();
        }
        else{
            //自己发布的
            $trip_data=Db::name('dd_publish_orders')->alias('o')
                ->join('iw_dd_users u','u.uid=o.driver_id')
                ->join('iw_dd_publish p','o.publish_id=p.id')
                ->field('u.headimgurl,u.nickname,u.trust_rank, u.car_info,u.tel,u.sex,
                            p.depart_time,p.status,p.region,p.begin_detail,p.begin_place,p.end_detail,p.end_place,
                            o.id,o.number')
                ->where('o.passenger_id',$this->uid)
                ->where('o.status',['=',0],['=',1],'or')
                ->find();
        }
        if(isset($trip_data)){
            //部分数据进行处理
            //获取今天凌晨的时间
            $today = strtotime(date("Y-m-d"),time());
            if($trip_data['depart_time']>$today){
                $trip_data['depart_time']="今天".date('H:i',$trip_data['depart_time']);
            }
            else{
                $trip_data['depart_time']= date('m-d H:i',$trip_data['depart_time']);
            }
        }
        else{
            $trip_data="";
        }
        $this->assign('trip_data',$trip_data);

        //判断是否绑定手机号
        $users_tel=Db::name('dd_users')->where('uid',$this->uid)->field('tel')->find();
        $this->assign('users_tel',$users_tel['tel']);

        //拿到用户类型
        $type =Db::name('dd_users')->where('uid', $this->uid)->field("type,uid")->find();
        $this->assign('type',$type);

        //拿到乘客发布中数据
        $publish_data=Db::name('dd_publish')->where('uid', $this->uid)
            ->where('is_del',0)
            ->where('status',0)
            ->where('type',1)
            ->find();
        if(isset($publish_data)){
            //部分数据进行处理
            $today = strtotime(date("Y-m-d"),time());
            if($publish_data['depart_time']>$today){
                $publish_data['depart_time']="今天".date('H:i',$publish_data['depart_time']);
            }
            else{
                $publish_data['depart_time']= date('m-d H:i',$publish_data['depart_time']);
            }
        }else{
            $publish_data="";
        }
        $this->assign('publish_data',$publish_data);

        //拿到附近车主(本市司机)信息
        $car_incity = []; $sum1=0;
        $car_incity1=Db::name('dd_publish')
            ->field('end_detail,count(end_detail) as co')
            ->where('type',2)
            ->where('status',0)
            ->where('region',0)
            ->group('end_detail')
            ->select();
        foreach ($car_incity1 as $k=>$sum){
            $sum1+=$sum['co'];
            if($k<6){
                $car_incity[$k] = $sum;
            }
        }
        $this->assign('sum1',$sum1);
        $this->assign('car_incity',$car_incity);

        //拿到跨城车主(跨城司机)信息
        $car_outcity = [];$sum2=0;
        $car_outcity1=Db::name('dd_publish')
            ->field('end_place,count(end_place) as co')
            ->where('type',2)
            ->where('status',0)
            ->where('region',1)
            ->group('end_place')
            ->select();
        foreach ($car_outcity1 as $k=>$sum){
            $sum2+=$sum['co'];
            if($k<6){
                $car_outcity[$k] = $sum;
            }
        }
        $this->assign('sum2',$sum2);
        $this->assign('car_outcity',$car_outcity);

        return $this->fetch();
    }

    //乘客发布
    public function add_publish(){
        if (\request()->isGet()){
            $data=input();
            //如果是取消成功跳转到发布（重新发布）,则需要带原参回到发布界面
            if (input('publish_id')){
                $data=Db::name('dd_publish')
                    ->field('begin_detail,begin_place,end_detail,end_place,region,lonlat_begin,lonlat_end')
                    ->where('id',input('publish_id'))
                    ->find();
                $this->assign('data',$data);
                return $this->fetch();
            }
            else{
                $this->assign('data',$data);
                return $this->fetch();
            }
        }
    //非正常途径进入则重定位到首页
        else{
            $this->redirect(url('index'));
        }
    }

    //等待接单
    public function wait_order(){
        if (request()->isPost()){
            //拿users表中的手机号
            $res=Db::name('dd_users')->where('uid',$this->uid)
                ->field('tel')
                ->find();
            $number_info = explode("-", input('number_info'));
            $ck=['adult'=>$number_info[0],'children'=>$number_info[1],'infant'=>$number_info[2]];
            $data=[
                'uid'=>$this->uid,
                'depart_time'=>strtotime(input('depart_time')),
                'content'=>input('content'),
                'type'=>1,
                'status'=>0,
                'begin_detail'=>input('begin_detail'),
                'begin_place'=>input('begin_place'),
                'end_detail'=>input('end_detail'),
                'end_place'=>input('end_place'),
                'number_info'=>$ck,
                'phone'=>$res['tel'],
                'lonlat_begin'=>input('lonlat_begin'),
                'lonlat_end'=>input('lonlat_end'),
                'region'=>input('region'),
            ];
            $passenger=new \app\index\model\lai\Passenger();
            $re = $passenger->insert_data($data);
            if($re){
                //跨城需要拿全部地址
                if($data['region']==1){
                    $order=[
                        'begin_detail'=>input('begin_detail'),
                        'begin_place'=>input('begin_place'),
                        'end_detail'=>input('end_detail'),
                        'end_place'=>input('end_place')
                    ];
                }else{
                    //本市只要拿详细地址即可
                    $order=[
                        'begin_detail'=>input('begin_detail'),
                        'begin_place'=>'',
                        'end_detail'=>input('end_detail'),
                        'end_place'=>'',
                    ];
                }
                //发布行程成功后，马上发送成功模板消息
                $this->publish_ok($re);
                $this->assign('publish_id',$re);
                $this->assign('order',$order);
                return $this->fetch();
            }else{
                $this->redirect('add_publish');
            }
        }
        else{
            if(input('publish_id')){
                $order1= Db::name('dd_publish')
                    ->where('id',input('publish_id'))
                    ->find();
                if(isset($order1)){
                    $this->assign('publish_id',$order1['id']);
                    $this->assign('order',$order1);
                    return $this->fetch();
                }else{
                    $this->redirect(url("index"));
                }
            }else{
                $this->redirect(url("index"));
            }
        }
    }

    //取消订单
    public function cancel_order(){
        if(input('publish_id')){
            $this->assign('publish_id',input('publish_id'));
            return $this->fetch();
        }else{
            $this->redirect(url("index"));
        }
    }

    //成功取消订单
    public function cancel_success(){
        if(input('publish_id')){
            $res=Db::name('dd_publish')->where('id',input('publish_id'))->update(['status'=>3]);
            if ($res){
                //取消发布行程发送模板
                $this->publish_cancle(input('publish_id'));
                $this->assign('publish_id',input('publish_id'));
                return $this->fetch();
            }else{
                $this->error('取消失败');
            }
        }else{
            if (input('publish')){
                $this->assign('publish_id',input('publish'));
                return $this->fetch();
            }else{
                $this->redirect(url('index'));
            }
        }
    }

    //行程中订单
    public function trip(){
        if(input('order_id')){
            $trip_data=Db::name('dd_publish_orders')->alias('o')
                ->join('iw_dd_users u','u.uid=o.driver_id')
                ->join('iw_dd_publish p','o.publish_id=p.id')
                ->field('u.headimgurl,u.nickname,u.trust_rank,u.car_info,u.tel,u.sex,
                                o.number,o.id,o.driver_id,o.status,
                                p.depart_time,p.begin_detail,p.begin_place,p.end_detail,p.end_place,
                                p.region,p.lonlat_begin,p.lonlat_end')
                ->where('o.id',input('order_id'))
                ->find();
            if($trip_data['driver_id']!=$this->uid){
                $trip_data['type'] = 1;
            }else{
                $trip_data['type'] = 2;
            }
            $today = strtotime(date("Y-m-d"),time());
            if($trip_data['depart_time']>$today){
                $trip_data['depart_time']="今天".date('H:i',$trip_data['depart_time']);
            }
            else{
                $trip_data['depart_time']= date('m-d H:i',$trip_data['depart_time']);
            }
            $trip_data['lonlat_begin'] = explode(',',$trip_data['lonlat_begin']);
            $trip_data['lonlat_end'] = explode(',',$trip_data['lonlat_end']);
            $this->assign('trip_data',$trip_data);
            return $this->fetch();
        }
        else{
            $this->redirect(url("index"));
        }
    }

    //市内车主
    public function car_incity(){
        if(input('city')){
            //此处拿的是发布的电话
            $car_incity=Db::name('dd_publish')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish.uid')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,
            iw_dd_users.car_info,iw_dd_publish.phone,iw_dd_users.sex,iw_dd_publish.id,
            iw_dd_publish.begin_place,iw_dd_publish.begin_detail,iw_dd_publish.end_place,
            iw_dd_publish.end_detail,iw_dd_publish.status,iw_dd_publish.depart_time,
            iw_dd_publish.number')
                ->where('iw_dd_publish.is_del',0)
                ->where('iw_dd_publish.status',0)
                ->where('iw_dd_publish.region',0)
                ->where('iw_dd_publish.type',2)
                ->where('iw_dd_publish.end_detail',input('city'))
                ->select();

        }else{
            $car_incity=Db::name('dd_publish')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish.uid')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,
            iw_dd_users.car_info,iw_dd_publish.phone,iw_dd_users.sex,iw_dd_publish.id,
            iw_dd_publish.begin_place,iw_dd_publish.begin_detail,iw_dd_publish.end_place,
            iw_dd_publish.end_detail,iw_dd_publish.status,iw_dd_publish.depart_time,
            iw_dd_publish.number')
                ->where('iw_dd_publish.is_del',0)
                ->where('iw_dd_publish.status',0)
                ->where('iw_dd_publish.region',0)
                ->where('iw_dd_publish.type',2)
                ->select();

        }
        foreach ($car_incity as $k=>$v){
            $today = strtotime(date("Y-m-d"),time());
            if($v['depart_time']>$today){
                $car_incity[$k]['depart_time']="今天".date('H:i',$v['depart_time']);
            }
            else{
                $car_incity[$k]['depart_time']= date('m-d H:i',$v['depart_time']);
            }
        }
        $this->assign('count',count($car_incity));
        $this->assign('car_incity',$car_incity);
        return $this->fetch();
    }

    //跨城车主
    public function car_outcity(){
        if(input('city')){
            $car_outcity=Db::name('dd_publish')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish.uid')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,iw_dd_users.car_info,iw_dd_publish.phone,iw_dd_users.sex,
                            iw_dd_publish.id,iw_dd_publish.begin_place,iw_dd_publish.begin_detail,iw_dd_publish.end_place,iw_dd_publish.end_detail,
                            iw_dd_publish.status,iw_dd_publish.depart_time,iw_dd_publish.number')
                ->where('iw_dd_publish.is_del',0)
                ->where('iw_dd_publish.status',0)
                ->where('iw_dd_publish.region',1)
                ->where('iw_dd_publish.type',2)
                ->where('iw_dd_publish.end_place',input('city'))
                ->select();
        }else{
            $car_outcity=Db::name('dd_publish')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish.uid')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,iw_dd_users.car_info,iw_dd_publish.phone,iw_dd_users.sex,
                            iw_dd_publish.id,iw_dd_publish.begin_place,iw_dd_publish.begin_detail,iw_dd_publish.end_place,iw_dd_publish.end_detail,
                            iw_dd_publish.status,iw_dd_publish.depart_time,iw_dd_publish.number')
                ->where('iw_dd_publish.is_del',0)
                ->where('iw_dd_publish.status',0)
                ->where('iw_dd_publish.region',1)
                ->where('iw_dd_publish.type',2)
                ->select();
        }


        foreach ($car_outcity as $k=>$v){

            $today = strtotime(date("Y-m-d"),time());
            if($v['depart_time']>$today){
                $car_outcity[$k]['depart_time']="今天".date('H:i',$v['depart_time']);
            }
            else{
                $car_outcity[$k]['depart_time']= date('m-d H:i',$v['depart_time']);
            }
        }
        $this->assign('count',count($car_outcity));
        $this->assign('car_outcity',$car_outcity);
        return $this->fetch();
    }

    //预约
    public function order(){
        if(input('publish_id')){
            //信息不完整
            $passenger_tel=Db::name('dd_users')->field('tel')->where('uid',$this->uid)->find();
            //如果该乘客order表有预约中订单
            $passenger_ordering=Db::name('dd_publish_orders')->where('passenger_id',$this->uid)->where('status',0)->find();
            //如果该司机publish信息状态不为0
            $driver_publish_status = Db::name('dd_publish')->field('status')->where('id',input('publish_id'))->find();
            //不能预约自己
            $dd_publish_uid= Db::name('dd_publish')->field('uid')->where('id',input('publish_id'))->find();
            if(!$passenger_tel){
                $this->error('信息不完整','sunfc.users/authuser');
            }
            if ($passenger_ordering){
                $this->error('有正在进行的订单');
            }
            if($driver_publish_status['status']!=0){
                $this->error('慢了一步呦');
            }
            if($dd_publish_uid['uid']==$this->uid){
                $this->error('不能预约自己呦');
            }
            Db::startTrans();
            try{
                //更新该司机publish信息数据
                Db::name('dd_publish')->where('id',input('publish_id'))->update(['status'=>1]);
                //插入order数据
                $driver_publish_msg = Db::name('dd_publish')->field('number,uid')->where('id',input('publish_id'))->find();
                $data = [
                    'order_id'=>date('YmdHis') . rand(1000,9999),
                    'passenger_id'=>$this->uid,
                    'driver_id'=>$driver_publish_msg['uid'],
                    'publish_id'=>input('publish_id'),
                    'number'=>$driver_publish_msg['number'],
                    'money'=>$driver_publish_msg['number']*5,
                    'status'=>0,
                    'create_time'=>time()
                ];
                $get_order_id  = Db::name('dd_publish_orders')->insertGetId($data);
                //给司机发送预约成功信息
                $get_order=Db::name('dd_publish_orders')->where('id',$get_order_id)->field('driver_id,publish_id')->find();
                $get_user= Db::name('dd_users')->where('uid',$get_order['driver_id'])->field('openid,realname')->find();
                $get_publish=Db::name('dd_publish')->where('id',$get_order['publish_id'])->find();
                if ($get_publish){
                    $weixin = new  \app\index\controller\Weixin();
                    $openid         =$get_user['openid'];
                    $re=$this->getWxTemplate();
                    $res=json_decode($re, true);
                    $templete_id    = $res['paidan_ok'];
                    $url            = Request()->domain().'/index/sunfc.driver/index';
                    $data_arr['user_name']                  = ['value'=>"亲爱的".$get_user['realname']."，您的订单已被成功预约",'color'=>'#ff8a5a'];
                    $data_arr['begin_place']                = ['value'=>$get_publish['begin_place'].$get_publish['begin_detail'],'color'=>'#1491cb'];
                    $data_arr['end_place']                  = ['value'=>$get_publish['end_place'].$get_publish['end_detail'],'color'=>'#1491cb'];
                    $data_arr['depart_time']                = ['value'=>date("Y-m-d H:i",$get_publish['depart_time']),'color'=>'#1491cb'];
                    $data_arr['number']                     = ['value'=>$get_publish['number'],'color'=>'#1491cb'];
                    $data_arr['content']                    = ['value'=>$get_publish['content'],'color'=>'#1491cb'];
                    $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr);
                    if($re['errcode'] != 0){
                        return $this->error("错误代码".$re['errcode']);
                    }
                }
                // 提交事务
                Db::commit();
                return $get_order_id;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }

        }else{
            $this->redirect('index');
        }
    }

    //取消还未上车订单
    public function cancel_ordering(){
        if(input('order_id')){
            Db::startTrans();
            try{
                //更新订单状态
                $data1 = Db::name('dd_publish_orders')
                    ->where('id',input('order_id'))
                    ->update(['status'=>-1]);
                //更新发布状态
                $type = Db::name('dd_publish_orders')
                    ->join('iw_dd_publish','iw_dd_publish.id=iw_dd_publish_orders.publish_id')
                    ->field('iw_dd_publish.type,iw_dd_publish.id')
                    ->where('iw_dd_publish_orders.id',input('order_id'))
                    ->find();
                //如果是乘客发布的
                if($type['type']==1){
                    $res = Db::name('dd_publish')
                        ->where('id',$type['id'])
                        ->update(['status'=>3]);
                }else{
                    //如果是司机发布的
                    $res = Db::name('dd_publish')
                        ->where('id',$type['id'])
                        ->update(['status'=>0]);
                }
                // 提交事务
                    Db::commit();
                    $this->publish_cancle1(input('order_id'));
                    return 1;

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
        }else{
            $this->redirect('index');
        }


    }


    public function up_car1(){

        // 参数验证
        if(input('order_id')){

            // 获取订单信息
            $order_info = Db::name('dd_publish_orders')->where('id',input('order_id'))->find();

            // 避免重复支付
            if ($order_info['status']==1||$order_info['status']==2){

                $this->error('已支付，无需重复支付');

            }else{

                $re = Db::name('dd_publish_orders')->where('id',input('order_id'))->update(['status'=>1]);

                $this->success("成功",'',$re);

            }
        }else{
            $this->error('参数有误');
        }
    }



    /**
     * 发起支付
     * URL：index/sunfc.passenger/up_car
     * return   $re        array        微信支付加密信息
     */
    public function up_car(){

        // 参数验证
        if(input('order_id')){

            // 获取订单信息
            $order_info = Db::name('dd_public_orders')->where('id',input('order_id'))->find();

            // 避免重复支付
            if ($order_info['status']==2||$order_info['status']==3){

                $this->error('已支付，无需重复支付');

            }else{

                // 获取用户openid
                $openid=Db::name('dd_users')->where('uid',$this->uid)->value('openid');

                // 调用支付接口
                $pay = new Pay();
                $re = $pay->pay($openid,$order_info['order_id'],$order_info['money'],url('pay/wxReturnOrders,["id"=>$order_id]'),'上车支付');

                $this->success("成功",'',$re);

            }
        }else{
            $this->error('参数有误');
        }
    }

    //下车
    public function down_car(){
        if(input('order_id')){
            Db::startTrans();
            try{
                $status= Db::name('dd_publish_orders')
                    ->where('id',input('order_id'))->find();
                if ($status['status']==2){
                    $this->success('订单已完成','sunfc.passenger/index');
                }
                //更新订单状态
                $data1 = Db::name('dd_publish_orders')
                    ->where('id',input('order_id'))
                    ->update(['status'=>2]);
                //更新发布状态
                $publish = Db::name('dd_publish_orders')->field('publish_id')->where('id',input('order_id'))->find();
                $data2 = Db::name('dd_publish')
                    ->where('id',$publish['publish_id'])
                    ->update(['status'=>2]);
                // 提交事务
                if($data1&&$data2&&$publish){
                    Db::commit();
                    return 1;
                }else{
                    return 0;
                }

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
        }else{
            $this->redirect('index');
        }


    }

    public function down_car_ok(){
        $status= Db::name('dd_publish_orders')
            ->where('id',input('order_id'))->find();
        if ($status['status']==2){
            $this->success('订单已完成');
        }else{
            $this->error();
        }
    }

    //    发布行程成功调用接口
    public function publish_ok($publish_id){
        $data_arr= Db::name('dd_users')->where('uid',$this->uid)->field('openid,realname')->find();
        $aa=Db::name('dd_publish')->where('id',$publish_id)->find();
        if ($aa){
            $weixin = new  \app\index\controller\Weixin();
            $openid         =$data_arr['openid'];

            $re=$this->getWxTemplate();
            $res=json_decode($re, true);
            $templete_id    = $res['add_publish'];
            $url            = Request()->domain().'/index/sunfc.passenger/wait_order?publish_id='.$publish_id;
            $data_arr1['user_name']                  = ['value'=>"亲爱的".$data_arr['realname']."，您的行程已发布，系统正努力为你寻找顺路车主，请耐心等待哟~",'color'=>'#ff8a5a'];
            $data_arr1['begin_place']                = ['value'=>$aa['begin_place'].$aa['begin_detail'],'color'=>'#1491cb'];
            $data_arr1['end_place']                 = ['value'=>$aa['end_place'].$aa['end_detail'],'color'=>'#1491cb'];
            $data_arr1['depart_time']                = ['value'=>date("Y-m-d H:i",$aa['depart_time']),'color'=>'#1491cb'];
            $data_arr1['number']                     = ['value'=>$aa['number'],'color'=>'#1491cb'];
            $data_arr1['content']                    = ['value'=>$aa['content'],'color'=>'#1491cb'];

            $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr1);
            if($re['errcode'] != 0){
                return $this->error("错误代码".$re['errcode']);
            }
        }
    }
    //    取消发布行程调用接口
    public function publish_cancle($publish_id){
        $data_arr= Db::name('dd_users')->where('uid',$this->uid)->field('openid,realname')->find();

        $aa=Db::name('dd_publish')->where('id',$publish_id)->find();
        if ($aa){
            $weixin = new  \app\index\controller\Weixin();
            $openid         = $data_arr['openid'];
            $re=$this->getWxTemplate();
            $res=json_decode($re, true);
            $templete_id    = $res['cancel_publish'];
            $url            = Request()->domain().'/index/sunfc.passenger/cancel_success?publish='.$publish_id;

            $data_arr1['user_name']                  = ['value'=>"亲爱的".$data_arr['realname']."，您的行程已取消，期待您的下次旅程呦~",'color'=>'#ff8a5a'];
            $data_arr1['begin_place']                = ['value'=>$aa['begin_place'].$aa['begin_detail'],'color'=>'#1491cb'];
            $data_arr1['end_place']                 = ['value'=>$aa['end_place'].$aa['end_detail'],'color'=>'#1491cb'];
            $data_arr1['cancel_time']                = ['value'=>date("Y-m-d H:i:s"),'color'=>'#1491cb'];

            $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr1);
            if($re['errcode'] != 0){
                return $this->error("错误代码".$re['errcode']);
            }
        }
    }

    //    预约行程取消接口
    public function publish_cancle1($order_id){

        $bb=Db::name('dd_publish_orders')->where('id',$order_id)->field('publish_id,driver_id')->find();

        $data_arr= Db::name('dd_users')->where('uid',$bb['driver_id'])->field('openid,realname')->find();

        $aa=Db::name('dd_publish')->where('id',$bb['publish_id'])->find();
        if ($aa){
            $weixin = new  \app\index\controller\Weixin();
            $openid         = $data_arr['openid'];
            $re=$this->getWxTemplate();
            $res=json_decode($re, true);
            $templete_id    = $res['cancel_publish'];
            $url            = Request()->domain().'/index/sunfc.driver/index';

            $data_arr1['user_name']                  = ['value'=>"亲爱的".$data_arr['realname']."，您的订单已被取消",'color'=>'#ff8a5a'];
            $data_arr1['begin_place']                = ['value'=>$aa['begin_place'].$aa['begin_detail'],'color'=>'#1491cb'];
            $data_arr1['end_place']                 = ['value'=>$aa['end_place'].$aa['end_detail'],'color'=>'#1491cb'];
            $data_arr1['cancel_time']                = ['value'=>date("Y-m-d H:i:s"),'color'=>'#1491cb'];

            $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr1);
            if($re['errcode'] != 0){
                return $this->error("错误代码".$re['errcode']);
            }
        }
    }

//    行程记录
    public function travel_records(){
        if(request()->isAjax()){
            $page = input('page')?input('page'):1;
            $trip_data1=Db::name('dd_publish_orders')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish_orders.driver_id')
                ->join('iw_dd_publish','iw_dd_publish_orders.publish_id=iw_dd_publish.id')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,
                iw_dd_users.car_info,iw_dd_users.tel,iw_dd_users.sex,iw_dd_publish_orders.create_time,iw_dd_publish_orders.money,
                iw_dd_publish_orders.number,iw_dd_publish.begin_place,iw_dd_publish_orders.id,
                iw_dd_publish.begin_detail,iw_dd_publish.end_place,iw_dd_publish.end_detail,
                iw_dd_publish.status,iw_dd_publish.region,iw_dd_publish_orders.status')
                ->where('iw_dd_publish_orders.passenger_id',$this->uid)
                ->page($page,4)
                ->select();
//            时间处理
            foreach ($trip_data1 as $k=>$v){
                $today = strtotime(date("Y-m-d"),time());
                if($v['create_time']>$today){
                    $trip_data1[$k]['create_time']="今天".date('H:i',$v['create_time']);
                }
                else{
                    $trip_data1[$k]['create_time']= date('m-d H:i',$v['create_time']);
                }
            }
/*            保险费处理
            foreach ($trip_data1 as $k=>$v){
                $trip_data1[$k]["money"]=$trip_data1[$k]["number"]*5;
            }*/
            return json(['code'=>1,'msg'=>'ok','data'=>$trip_data1]);
        }else{
            return $this->fetch();
        }
    }
    public  function driver_log(){
        if(request()->isAjax()){
            $page = input('page')?input('page'):1;
            $trip_data2=Db::name('dd_publish_orders')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish_orders.passenger_id')
                ->join('iw_dd_publish','iw_dd_publish_orders.publish_id=iw_dd_publish.id')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,
                iw_dd_users.car_info,iw_dd_users.tel,iw_dd_users.sex,iw_dd_publish_orders.create_time,iw_dd_publish_orders.money,
                iw_dd_publish_orders.number,iw_dd_publish.begin_place,iw_dd_publish_orders.id,
                iw_dd_publish.begin_detail,iw_dd_publish.end_place,iw_dd_publish.end_detail,
                iw_dd_publish.status,iw_dd_publish.region,iw_dd_publish_orders.status,iw_dd_publish.lonlat_begin,iw_dd_publish.lonlat_end')
                ->where('iw_dd_publish_orders.driver_id',$this->uid)
                ->page($page,5)
                ->select();

            foreach ($trip_data2 as $k=>$v){
                $today = strtotime(date("Y-m-d"),time());
                if($v['create_time']>$today){
                    $trip_data2[$k]['create_time']="今天".date('H:i',$v['create_time']);
                }
                else{
                    $trip_data2[$k]['create_time']= date('m-d H:i',$v['create_time']);
                }
            }
            foreach ($trip_data2 as $k=>$v){
                $trip_data2[$k]["money"]=$trip_data2[$k]["number"]*5;
            }
            return json(['code'=>1,'msg'=>'ok','data'=>$trip_data2]);
        }else{
            return $this->redirect('index');
        }
    }


}